<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import * as echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({
                 collection1,
                 collection2,
                 collection3,
                 collection4,
                 collection5,
                 collection6,
                 collection7,
                 collection8,
                 collection9,
                 collection10,
               } = {}) {
      this.chart.setOption({
        title: {
          text: '1-12月份', // 添加标题文本
          left: 'center', // 标题水平居中
          top: 'top', // 标题顶部对齐
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 10,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        series: [{
          name: '通威股份',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: collection1,
          animationDuration
        }, {
          name: '隆基绿能',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: collection2,
          animationDuration
        }, {
          name: '特变电工',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: collection3,
          animationDuration
        }
          , {
            name: '晶科能源',
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            data: collection4,
            animationDuration
          }, {
            name: '天合光能',
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            data: collection5,
            animationDuration
          }, {
            name: '晶奥科技',
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            data: collection6,
            animationDuration
          }, {
            name: 'TCL中环',
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            data: collection7,
            animationDuration
          }, {
            name: '正泰电器',
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            data: collection8,
            animationDuration
          }, {
            name: '阳光能源',
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            data: collection9,
            animationDuration
          }, {
            name: '协鑫科技',
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            data: collection10,
            animationDuration
          }
        ]
      })
    }
  }
}
</script>
